<?php
namespace Common\Model;
use Think\Model;

/**
 * Class CsdtMachineModel
 * @package Common\Model
 * 调蓄池格栅机
 */
class CsdtMachineModel extends BaseModel{
    public $pix = "";
    public $fieldsStr = '*';
    public $orderStr = 'ps_id desc,scrmach_number asc';

    public function field_array(){
        /**
         * type:
         * 0，不显示字段
         * 1，文本框
         * 2，下拉框
         * 3，数值查询，带条件
         * 4，日期查询，带条件
         */
        $array = array(
            'base' => array(
                'text' => '基本信息',
                'children'=>array(
                    array('field'=>'ps_id','text' => '','type'=>0),
                    array('field'=>'scrmach_id','text' => '格栅除污机编码','type'=>1),
                    array('field'=>'scrmach_name','text' => '格栅除污机名称','type'=>1),
                    array('field'=>'scrmach_mfr','text' => '生产厂家','type'=>1),
                    array('field'=>'scrmach_model','text' => '型号','type'=>1),
                    array('field'=>'scrmach_motor_power','text' => '电机功率(千瓦)','type'=>3,'condition'=>get_condition_array()),
                    array('field'=>'scrmach_dep','text' => '格栅井深(米)','type'=>3,'condition'=>get_condition_array()),
                    array('field'=>'scrmach_wid','text' => '格栅宽度(米)','type'=>3,'condition'=>get_condition_array()),
                    array('field'=>'scrmach_svc_dt','text' => '投运日期','type'=>4,'condition'=>get_condition_array()),
                ),
            ),
        );
        return $array;
    }

    public function get_list_use_page($map=array(), $cur_page, $rows_num){
        $map_filter = filter_query_map_get_order_str($map, $this->orderStr);
        $map = $map_filter['map'];
        $order_str = $map_filter['order_str'];
        $map = filter_field_array($this->field_array(), $map);
        $count = M('CsdtMachine')->where($map)->count();
        $list = $this->where($map)->order($order_str)->page($cur_page.",$rows_num")->select();
        $station_model = D('Csdt');
        foreach($list as $k => $v){
            $sta_temp = $station_model->queryObj($v['ps_id'], 'id');;
            $list[$k]['sta_name'] = $sta_temp['ps_name'];
            $list[$k]['type'] = pump_machine_type_option($list[$k]['type'], false);
        }
        return array('total'=>$count, 'list'=>$list);
    }

    public function gen_search($map, $page, $rows_num){
        $station_model = D('Csdt');
        $record_model = D('CsdtMachineRecord');
        $station_ids = null;
        $status = '';
        $p_type = '';
        $f_id = '';
        if(isset($map['ps_id'])){
            $station_ids = my_array_filter_to_text(str_replace('%', '', $map['ps_id'][1]), ',');
            unset($map['ps_id']);
        }
        if(isset($map['f_id'])){
            $f_id = str_replace('%', '', $map['f_id'][1]);
            $f_id = array_filter(explode(',', $f_id));
            unset($map['f_id']);
        }
        if(isset($map['status'])){
            $status = str_replace('%', '', $map['status'][1]);
            unset($map['status']);
        }
        if($station_ids){
            $stations = $station_model->order('ps_name desc,mobile desc')->select($station_ids);
        }else{
            $stations = $station_model->order('ps_name desc,mobile desc')->select();
        }
        $fys = array();
        //查询出所有设备
        foreach($stations as $station){
            $query_map['ps_id'] = array('eq', $station['id']);
            $fys = array_merge($fys, $this->where($query_map)->order($this->orderStr)->select());
        }
        $result = array();
        $all_fys = array();
        $error_list = array();
        foreach($fys as $k => $fy){
            if(!in_array($fy['id'], $f_id)&&!empty($f_id))
                continue;
            $fy_status =  $record_model->get_last_one($fy['id']);
            if($fy_status == 999 || $fy_status == -1)
                $fy_status = 0;
            if($status == '' || $fy_status == $status){
                $fys[$k]['status'] = $fy_status;
                if($fy_status == -2)
                    $error_list[] = $fy;
                else
                    $all_fys[] = $fy;
            }
        }
        $result = array_merge($error_list, $all_fys);
        $all_fys = array_slice($result, ($page-1)*$rows_num, $rows_num);
        foreach($all_fys as $k => $fy){
            $last_record = $record_model->get_last_one_record($fy['id']);
            $fy_status =  $last_record['status'];
            if($fy_status == 999 || $fy_status == -1 || $fy_status == null)
                $fy_status = 0;
            if($status == '' || $fy_status == $status){
                $sta_temp = $station_model->queryObj($fy['ps_id'], 'id');
                $all_fys[$k]['sta_name'] = $sta_temp['ps_name'];
                $all_fys[$k]['sta_id'] = $sta_temp['id'];
                $all_fys[$k]['work_person'] = D('MobileAdmin')->get_working_names_by_ids($sta_temp['ps_opr']);
                $all_fys[$k]['status_text'] = device_status_option($fy_status, false);
                if($fy_status==0){
                    $time = strtotime($last_record['mac_off'])-strtotime($last_record['mac_start']);
                    $all_fys[$k]['work_time'] = round(timestamp_to_time('hour', $time), 2);
                }else if($fy_status==1){
                    $time = strtotime(getNowTime())-strtotime($last_record['mac_start']);
                    $all_fys[$k]['work_time'] = round(timestamp_to_time('hour', $time), 2);
                }else if($fy_status < 0){
                    $all_fys[$k]['error_time'] = $last_record['date'];
                }
                $all_fys[$k]['status'] = $fy_status;
                $all_fys[$k]['mac_start'] = $last_record['mac_start'];
                $all_fys[$k]['mac_off'] = $last_record['mac_off'];
                $all_fys[$k]['work_log'] = '<a href="#" onclick="open_easyui_window_with_id(\'log_window\', \''.$sta_temp['ps_name'].'-'.$fy['scrmach_name'].'-'.$fy['scrmach_number'].'\', \'/Manage/AdminCsdtMachine/just_see?fy_id='.$fy['id'].'\')">查看</a>';
            }
        }
        $footer = array(array('scrmach_number'=>'格栅机总数', 'mac_start'=>count($result)));
        return array('total'=>count($result), 'rows'=>$all_fys, 'footer'=>$footer);
    }

    public function filter_all(){
        $station_model = D('Csdt');
        $list = $this->getAll();
        foreach($list as $k => $v){
            $fa = $station_model->queryObj($list[$k]['ps_id'], 'id');
            $list[$k]['ps_id'] = $fa['ps_name'];
        }
        return $list;
    }

    public function query_by_pids($pids, $fields=''){
        $log_model = D('CsdtMachineRecord');
        $station_model = D('Csdt');
        $temp = array();
        foreach($pids as $pid){
            $station = $station_model->queryObj($pid, 'id');
            $map['ps_id'] = array('eq', $pid);
            if(!$fields)
                $list = $this->where($map)->order($this->orderStr)->select();
            else
                $list = $this->field($fields)->where($map)->order($this->orderStr)->select();
            foreach($list as $pump){
                $pump['station'] = $station['ps_name'];
                $pump['model'] = $this->getModelName();
                $pump['statuss'] = filter_device_status($log_model->get_last_one($pump['id']));
                $pump['status'] = $log_model->get_last_one($pump['id']);
                $temp[] = $pump;
                unset($pump);
            }
        }
        return $temp;
    }

    public function get_json_obj($id){
        $art = array(
            'station' => 'Csdt',
            'log' => 'CsdtMachineRecord',
            'pid_field' => 'ps_id',
            'sta_name_field' => 'ps_name',
        );
        $station_model = D($art['station']);
        $log_model = D($art['log']);
        $obj = $this->queryObj($id, 'id');
        $station = $station_model->queryObj($obj[$art['pid_field']], 'id');
        $obj['station'] = $station[$art['sta_name_field']];
        $obj['statuss'] = filter_device_status($log_model->get_last_one($obj['id']));
        $obj['status'] = $log_model->get_last_one($obj['id']);
        return json_encode($obj);
    }

    public function q_s($pid){
        $return = -99;
        $record_model = D('CsdtMachineRecord');
        $list = $this->queryField('ps_id',$pid);
        foreach($list as $v){
            $status = $record_model->get_last_one($v['id']);
            if($status < 0){
                $return = -1;
                break;
            }
        }
        return $return;
    }

    public function deleteArray($ids=array()){
        $return = '';
        if(!is_array($ids)){
            $ids = array_filter(explode(',', $ids));
        }
        $fys_models = array(
            'CsdtMachineRecord',
        );
        foreach($ids as $id){
            foreach($fys_models as $fy_model){
                $map['macid'] = array('eq', $id);
                D($fy_model)->where($map)->delete();
            }
            $return = $this->delete($id);
        }
        return $return;
    }

    public function get_infor_text($id){
        $one = $this->find($id);
        $station = D('Csdt')->queryObj($one['ps_id'], 'id');
        return array('text'=>$one['scrmach_number'].'，'.$one['scrmach_model'], 'phone'=>$station['mobile']);
    }
}